<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"
    import="java.sql.*"%>
<jsp:useBean id="utilities" class="masterpiece.util.Utilities" scope="session" />    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Search Archive</title>
<link rel="stylesheet" type="text/css" href="css/default.css" />
</head>
<body>
<%@ include file="top_menu.jsp" %>
  <%
  String headlineKeyParam = request.getParameter("headlineKey");
  if (headlineKeyParam == null)
	  headlineKeyParam = "";
  String articleDateParam = request.getParameter("articleDate");
  if (articleDateParam == null)
	  articleDateParam = "";
  String issueDateParam = request.getParameter("issue");
  if (issueDateParam == null)
	  issueDateParam = "";
  %>
  <form action="search.jsp" method="get">
    <h1>Search Archive</h1>
    <dl class="wide">
      <dt>Headline key:</dt>
      <dd><input type="text" name="headlineKey" value="<%=headlineKeyParam%>" /></dd>
      <dt>Section:</dt>
      <dd>
        <%
        String sec = request.getParameter("section");
        if (sec == null)
        	sec = "-1";
        %>
        <select name="section">
          <option <%=(sec.equals("-1") ? "selected=\"selected\"" : "")%> value="-1">Any</option>
          <option <%=(sec.equals("1") ? "selected=\"selected\"" : "")%>value="1">1 &ndash; News</option>
          <option <%=(sec.equals("2") ? "selected=\"selected\"" : "")%>value="2">2 &ndash; Op-Ed</option>
          <option <%=(sec.equals("3") ? "selected=\"selected\"" : "")%>value="3">3 &ndash; LuteLife</option>
          <option <%=(sec.equals("4") ? "selected=\"selected\"" : "")%>value="4">4 &ndash; International</option>
          <option <%=(sec.equals("5") ? "selected=\"selected\"" : "")%>value="5">5 &ndash; Arts &amp; Entertainment</option>
          <option <%=(sec.equals("6") ? "selected=\"selected\"" : "")%>value="6">6 &ndash; Sports</option>
        </select>
      </dd>
      <dt>Article date:</dt>
      <dd><input type="text" name="articleDate" value="<%=articleDateParam%>" /></dd>
      <dt>Issue date:</dt>
      <dd>
        <select name = "issue">
          <option value="&lt;null&gt;">Any</option>
        <%
        ResultSet issues = utilities.returnIssueDates();
        while (issues.next()){
        	String issuestr = issues.getDate(1).toString();
        %>
          <option <%=(issuestr.equals(issueDateParam) ? "selected=\"selected\"" : "")%>><%=issuestr%></option>
        <%}%>
        </select>
      </dd>
    </dl>
    
    <p class="buttons">
      <input type="submit" value="Search" />
    </p>
  </form>
  
  <%
  ResultSet rs = null;
  
  int section;
  try {
	  section = Integer.parseInt(request.getParameter("section"));
  } catch (NumberFormatException e) {
	  section = -1;
  }
  
  java.sql.Date articleDate = null;
  String articleDateStr = request.getParameter("articleDate");
  if (articleDateStr != null && !articleDateStr.equals("")) {
	  try {
		  articleDate = java.sql.Date.valueOf(articleDateStr);
	  } catch (IllegalArgumentException e) {
		  out.print("<p class=\"message error\">Invalid article date: "+e.getMessage()+".<br/>Dates must be in the format YYYY-MM-DD.</p>");
	  }
  }
  
  java.sql.Date issueDate = null;
  String issueDateStr = request.getParameter("issue");
  if (issueDateStr != null && !issueDateStr.equals("") && !issueDateStr.equals("<null>")) {
    try {
    	issueDate = java.sql.Date.valueOf(issueDateStr);
    } catch (IllegalArgumentException e) {
      out.print("<p class=\"message error\">Invalid issue date: "+e.getMessage()+".<br/>Dates must be in the format YYYY-MM-DD.</p>");
    }
  }
  
	try {
	  rs = utilities.searchArchive(section,
			                           articleDate,
    	                           request.getParameter("headlineKey"),
    	                           issueDate);
  } catch (Exception e) {
    out.print("<p class=\"message error\">Error: "+e.getMessage()+"</p>");
  }
  
	%>
  
  <% if(rs != null) { %>
  <%   if(rs.next()) { %>
  <table>
    <thead>
      <tr>
        <td>ID&nbsp;#</td>
        <td>Section&nbsp;#</td>
        <td>Headline</td>
        <td>Sub-Headline</td>
        <td>Article Date</td>
        <td>Issue Date</td>
      </tr>
    </thead>
    <tbody>
      <% do { %>
      <tr>
        <td><a href="article.jsp?id=<%=rs.getInt("id")%>"><%=rs.getInt("id")%></a></td>
        <td><%=rs.getInt("section")%></td>
        <td><%=rs.getClob("headline").getSubString(1l,(int)rs.getClob("headline").length())%></td>
        <td><%=rs.getClob("sub_headline").getSubString(1l,(int)rs.getClob("sub_headline").length())%></td>
        <td><%=rs.getDate("article_date")%></td>
        <td><%=rs.getDate("issue")%></td>
      </tr>
      <% } while(rs.next()); %>
    </tbody>
  </table>
  <%   } else { %>
  <p class="message info">There are no results to display.</p>
  <%   } %>
  <% } %>
</body>
</html>